package org.aeon.client.handler.outbound;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.message.request.RequestMessage;

/**
 * @author: MissingAeon
 * @date: 2024/1/27 15:22
 * @description:
 */
@Slf4j
@ChannelHandler.Sharable
public class MessageTokenHandler extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof RequestMessage message) {
            if (GlobalContext.getContext().getToken() != null) {
                message.setToken(GlobalContext.getContext().getToken());
            } else if (GlobalContext.getContext().getCurrentUser() != null) {
                // message.setUserVerification();
            }
        }
        super.write(ctx, msg, promise);
    }
}
